package rtyryry;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.Node;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestDOM {
	public static void main (String args[]){
		try{
			DocumentBuilderFactory  dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db =  dbf.newDocumentBuilder();
			Document doc= db.parse("vehicles.xml");
			NodeList vehicles= (NodeList) doc.getChildNodes().item(0);
			NodeList vChildNodes= ((org.w3c.dom.Node) vehicles).getChildNodes();
			int count=0;
			for(int i=0;i<vChildNodes.getLength();i++){
				if(vChildNodes.item(i).getNodeType()==Node.ELEMENT_NODE){
					count++;
				}
			}
			System.out.println("");
			NodeList truckList  = doc.getElementsByTagName("truck");
			System.out.println();
			for(int i=0;i<truckList.getLength();i++){
				Node truck= (Node) truckList.item(i);
				Element element = (Element)truck;
				String idValue= element.getAttribute("id");
				System.out.println();
				for(Node node= (Node) truck.getFirstChild();node!= null;node= (Node) node.getNextSibling()){
					if(node.getNodeType()==Node.ELEMENT_NODE){
						String name= node.getNodeName();
						String value= node.getFirstChild().getNodeValue();
						System.out.println();
						
					}
				}
			}
		}catch(ParserConfigurationException e){
			e.printStackTrace();
		}catch(SAXException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}

}
